package com.dc.docs._888_面试.concurrent._6_queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Description: 阻塞队列 Demo
 * @author: gdc
 * @date: 2021/6/16
 * @version 1.0
 */
public class ArrayBlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        /*
         * 1、抛出异常 add()/remove()
         */
        //System.out.println(queue.add("a"));
        //System.out.println(queue.add("b"));
        //System.out.println(queue.add("c"));
        //System.out.println(queue.element()); //检查队首元素
        //System.out.println(queue.remove());
        //System.out.println(queue.remove());
        //System.out.println(queue.remove());

        /*
         * 2、返回布尔类型 offer()/pull()
         */
        //System.out.println(queue.offer("a"));
        //System.out.println(queue.offer("b"));
        //System.out.println(queue.offer("c"));
        //System.out.println(queue.peek()); //检查队首元素
        //System.out.println(queue.poll());
        //System.out.println(queue.poll());
        //System.out.println(queue.poll());

        /*
         * 3、阻塞 put()/take()
         */
        //queue.put("a");
        //queue.put("b");
        //queue.put("c");
        //System.out.println(queue.take());
        //System.out.println(queue.take());
        //System.out.println(queue.take());

        /*
         *4、超时
         */
        System.out.println(queue.offer("a",2L, TimeUnit.SECONDS));
        System.out.println(queue.offer("b",2L, TimeUnit.SECONDS));
        System.out.println(queue.offer("c",2L, TimeUnit.SECONDS));
    }
}
